package com.netease.xone.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.EditText;

import com.netease.xone.dataMgr.EmoticonMgr1;

/**
 * 简单定制的EditText。
 * 
 * @see EditText
 */
public class EmoticonEdit extends EditText {

	public EmoticonEdit(Context context) {
		super(context);
	}

	public EmoticonEdit(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public EmoticonEdit(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		addTextChangedListener(mTextChange);
	}

	TextWatcher mTextChange = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			int selBegin = getSelectionStart();
			int selEnd = getSelectionEnd();
			int[] newSelect = new int[] { selBegin, selEnd };
			EmoticonMgr1.getInstance(getContext()).setEmoticonSpan(s, newSelect);
			if (newSelect[0] != selBegin || newSelect[1] != selEnd)
				setSelection(newSelect[0], newSelect[1]);
		}
	};

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			ViewParent parent = getParent();
			if (parent != null)
				parent.requestDisallowInterceptTouchEvent(true);
		}
		return super.onTouchEvent(ev);
	}
}
